package com.jdd.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.RedisUtil;
import com.jdd.common.util.ResultGenerator;
import com.jdd.dao.entity.TArea;
import com.jdd.dao.entity.TProject;
import com.jdd.dao.mapper.SysDepartAreaMapper;
import com.jdd.dao.mapper.TAreaMapper;
import com.jdd.dao.mapper.TProjectMapper;
import com.jdd.dao.service.TProjectService;
import com.jdd.modules.system.mapper.SysUserRoleMapper;
import com.jdd.modules.system.service.ISysUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.modules.system.entity.SysDepart;
import com.jdd.modules.system.entity.SysUser;
import com.jdd.modules.system.entity.SysUserDepart;
import com.jdd.modules.system.entity.dto.BindGroupReq;
import com.jdd.modules.system.entity.vo.UserDeptVo;
import com.jdd.modules.system.enums.OrgTypeEnum;

import com.jdd.modules.system.mapper.SysUserDepartMapper;
import com.jdd.modules.system.model.DepartIdModel;

import com.jdd.modules.system.service.ISysUserDepartService;
import com.jdd.modules.system.service.ISysUserService;

/**
 * <P>
 * 用户部门表实现类
 * <p/>
 * @Author ZhiLin
 *@since 2019-02-22
 */
@Slf4j
@Service
public class SysUserDepartServiceImpl extends ServiceImpl<SysUserDepartMapper, SysUserDepart> implements ISysUserDepartService {

	@Autowired
	private ISysUserService sysUserService;
	@Resource
	private SysUserDepartMapper userDepartMapper;
	@Resource
	private SysDepartAreaMapper sysDepartAreaMapper;
	@Resource
	private TAreaMapper tAreaMapper;
	@Resource
	private TProjectMapper tProjectMapper;
	@Resource
	private SysUserRoleMapper  sysUserRoleMapper;


	@Override
	public List<String> getUserProjectCode(LoginUser loginUser) {
		List<String> roleCodes = sysUserRoleMapper.getRoleByUserName(loginUser.getUsername());
		List<SysUserDepart> sysUserDepartList = userDepartMapper.getUserDepartByUid(loginUser.getId());
		log.info("sysUserDepartList:{}",sysUserDepartList);
		List<String> projectIds = new ArrayList<>();
		List<String> areaIds = new ArrayList<>();
		if(!roleCodes.isEmpty())
		{
			if(roleCodes.get(0).equals("projectAdminRole"))
			{
			  return tProjectMapper.getIdsByProjectCode(loginUser.getOrgCode());
			}
		}
		if(!roleCodes.isEmpty())
		{
			/*如果是系统管理员*/
			if(roleCodes.get(0).equals("admin"))
			{
				QueryWrapper<TProject> queryWrapper	= new QueryWrapper<>();
				List<TProject> tProjects = tProjectMapper.selectList(queryWrapper);
				for (TProject tProject : tProjects) {
					projectIds.add(tProject.getId());
				}
				return projectIds;
			}
		}

		for(SysUserDepart sysUserDepart : sysUserDepartList)
		{
			if(sysUserDepart.getType()==0)
			{
				projectIds.add(sysUserDepart.getDepId());
			}else {
				areaIds.add(sysUserDepart.getDepId());
			}
		}
		this.appendProjectId(areaIds,projectIds);
		return projectIds;
	}

	@Override
	public boolean removeBind(String id) {
		return userDepartMapper.removeBind(id);
	}

	private void appendProjectId(List<String> areaIds,List<String> projectId){
		if(CollUtil.isNotEmpty(areaIds))
		{
			List<TProject> projectList = tProjectMapper.selectByAreaIds(areaIds);
			for(TProject tProject:projectList)
			{
				if(!projectId.contains(tProject.getId()))
				{
					projectId.add(tProject.getId());
				}
			}
			List<String> childAreaId = sysDepartAreaMapper.getChildAreaIdByAreaIds(areaIds);
			this.appendProjectId(childAreaId,projectId);
		}
	}

}
